package com.offroader.http;

import org.apache.http.HttpResponseInterceptor;
import org.apache.http.client.params.ClientPNames;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpParams;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

public class HttpHelper {
	private static final HttpHelper instance = new HttpHelper();
	private AsyncHttpClient asyncHttpClient;

	private HttpHelper() {
		asyncHttpClient = new AsyncHttpClient();
		DefaultHttpClient httpClient = (DefaultHttpClient) asyncHttpClient.getHttpClient();
		httpClient.addRequestInterceptor(new BaseHttpRequestInterceptor());
		httpClient.addResponseInterceptor(new BaseHttpResponseInterceptor());

		HttpParams params = httpClient.getParams();
		params.setParameter(ClientPNames.HANDLE_REDIRECTS, false);

	}

	public static HttpHelper getInstance() {
		return instance;
	}

	public void get(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
		asyncHttpClient.get(url, params, responseHandler);
	}

	public void post(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
		asyncHttpClient.post(url, params, responseHandler);
	}
	
	public void addResponseInterceptor(HttpResponseInterceptor responseInterceptor){
		DefaultHttpClient httpClient = (DefaultHttpClient) asyncHttpClient.getHttpClient();
		httpClient.addResponseInterceptor(responseInterceptor);
	}

}
